/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import org.jfugue.Pattern;
import org.jfugue.Player;

/**
 *
 * @author Rey
 */
public class Reproducir extends Thread {

    private Player player = new Player();
    Pattern reproducir;
    boolean terminar;

    public Reproducir(Pattern reproducir) {
        
        this.reproducir = reproducir;
    }
    
    public void ReproducirSonidos()
    {
        File midifile=null;
        
        int random = 1+((int)Math.random()*4);
        
        switch(random)
        {
            case 1:
                midifile = new File(Efectos.SONIDO1);
                break;
            case 2:
                midifile = new File(Efectos.SONIDO2);
                break;
            case 3:
                midifile = new File(Efectos.SONIDO3);
                break;
            case 4:
                midifile = new File(Efectos.SONIDO4);
                break;
        }
        
        
        try {
            this.reproducir = player.loadMidi(midifile);
            
        } catch (IOException ex) {
            Logger.getLogger(Reproducir.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidMidiDataException ex) {
            Logger.getLogger(Reproducir.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void ReproducirFinal()
    {
        File midifile=null;
        
        int random = 1+((int)Math.random()*4);
        
        switch(random)
        {
            case 1:
                midifile = new File(Efectos.FINAL1);
                break;
            case 2:
                midifile = new File(Efectos.FINAL2);
                break;
            case 3:
                midifile = new File(Efectos.FINAL3);
                break;
            case 4:
                midifile = new File(Efectos.FINAL4);
                break;
        }
        
        
        try {
            this.reproducir = player.loadMidi(midifile);
            
        } catch (IOException ex) {
            Logger.getLogger(Reproducir.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidMidiDataException ex) {
            Logger.getLogger(Reproducir.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    @Override
    public void run() {
        terminar = false;
        while (terminar == false) {
            getPlayer().play(reproducir);
            if (getPlayer().isFinished()) {
                terminar = true;
            }
        }
    }

    public void Detener() {
        if (getPlayer().isStarted()) {
            getPlayer().stop();
            terminar = true;
        }
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }
    
    public void GuardarCancion(Pattern pat, String file)
    {
        try {
                
                String sinext = file.substring(0,file.length()-5);
                
                player.saveMidi(pat, new File(sinext+".midi"));
                
            } catch (IOException ex) {
                Logger.getLogger(Reproducir.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("error");
            }
    }
}
